package com.xykj.web.controller;

import com.xykj.common.response.Response;
import com.xykj.pojo.dto.HKFundFlowDto;
import com.xykj.service.HKFundFlowService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/hk-fund-flow")
public class HKFundFlowController {
    @Autowired
    private HKFundFlowService hkFundFlowService;
    @RequestMapping("/getHKFundFlow")
    public Response queryHKFundFlow(@RequestBody HashMap<String,String> map) {
        String dateStr = map.get("dateStr");
        if (dateStr == null||dateStr.isEmpty()) {
            throw new IllegalArgumentException("日期参数不能为空");
        }
        System.out.println(dateStr);
        Date date = parseDate(dateStr);
        if (date.after(new Date(System.currentTimeMillis()))){
            throw new IllegalArgumentException("日期不能大于当前日期");
        }
        List<HKFundFlowDto> hkFundFlowDtos = hkFundFlowService.queryHKFundFlow(date);
        return Response.success("查询成功",hkFundFlowDtos);

    }
    private Date parseDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new IllegalArgumentException("日期格式必须为 yyyy-MM-dd");
        }
    }
}
